<template>
  <div class="user-info">
    <!-- 个人信息 -->
    <el-form ref="userInfoForm" :model="userInfo" label-width="220px">
      <!--手机 -->

      <el-form-item label="手机">
        <el-input v-model="userInfo.mobile" style="width: 220px" />
      </el-form-item>

      <!-- 工号 入职时间 -->

      <el-form-item label="入职时间">
        <el-date-picker
          v-model="userInfo.timeOfEntry"
          type="date"
          class="inputW"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>

      <!-- 员工照片 -->

      <el-form-item label="员工头像">
        <!-- 放置上传图片 -->
        <img>
      </el-form-item>

      <!-- 保存个人信息 -->
      <el-form-item>
        <el-button type="primary" @click.native="hSubmit">保存更新</el-button>
        <el-button @click="$router.back()">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { getUserInfoById, updateUserInfo } from '@/api/employees'
export default {
  data() {
    return {
      userInfo: {
        mobile: '',
        timeOfEntry: ''
      }
    }
  },
  created() {
    this.loadUserInfoById()
  },
  methods: {
    // 获取用户基本信息
    async loadUserInfoById() {
      const res = await getUserInfoById(this.$route.params.id)
      // console.log(res)
      // 数据回填到表单
      this.userInfo = res.data
      console.log(this.userInfo)
    },
    // 用户点击保存更新
    hSubmit() {
      // 1.表单校验
      this.$refs.userInfoForm.validate(async(valid) => {
        if (valid) {
          // 2.调用接口 更新用户信息
          const res = await updateUserInfo(this.userInfo)
          console.log(res)
          // 提示成功
          this.$message.success('更新成功')
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>
<style scoped lang="scss">
.user-info {
  padding-top: 20px;
}
</style>
